package net.oschina.aibinxiao.security.des;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

/**
 * @Author: AiBinXiao
 * @Description: 3重DES对称加密算法不同的实现
 * @Date: 2017/10/13 16:13
 */

public class My3DES {

    private static String src = "imooc security 3des";

    public static void main(String[] args) {
        jdk3DES();
        bc3DES();
    }

    // 3重DES对称加密算法 JDK实现
    public static void jdk3DES() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey(); // 生成密钥
            byte[] bytesKey = secretKey.getEncoded();

            // KEY转换
            DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
            Key convertKey = factory.generateSecret(desKeySpec);

            // 加密
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); // 加密算法/工作模式/填充方式
            cipher.init(Cipher.ENCRYPT_MODE, convertKey);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("JDK 3DES encrypt : " + Hex.encodeHexString(result));
            // JDK DES encrypt : 1f1f1227dc8e232cfe9d9816bc4ddca18b24c1aea61ca4f3

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, convertKey);
            result = cipher.doFinal(result);
            System.out.println("JDK 3DES decrypt : " + new String(result));
            // JDK DES decrypt : imooc security des
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 3重DES对称加密算法 Bouncy Castle实现
    public static void bc3DES() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede", "BC");
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey(); // 生成密钥
            byte[] bytesKey = secretKey.getEncoded();

            // KEY转换
            DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
            Key convertKey = factory.generateSecret(desKeySpec);

            // 加密
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); // 加密算法/工作模式/填充方式
            cipher.init(Cipher.ENCRYPT_MODE, convertKey);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("Bouncy Castle 3DES encrypt : " + Hex.encodeHexString(result));
            // JDK DES encrypt : 1f1f1227dc8e232cfe9d9816bc4ddca18b24c1aea61ca4f3

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, convertKey);
            result = cipher.doFinal(result);
            System.out.println("Bouncy Castle 3DES decrypt : " + new String(result));
            // JDK DES decrypt : imooc security des
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
